/*
Startup controller Library by Mark Medrano 2012
Licensed under the MIT license: http://www.opensource.org/licenses/mit-license.php

*/

#ifndef STARTUP_CONTROLLER_LIB
#define STARTUP_CONTROLLER_LIB

#include <inttypes.h>
#include "WProgram.h"
#include <StartupControllerCommands.h>
#include <SPI.h>
#include "pins_arduino.h"

//#include "UtilityCard.h"
//#include "UtilityFat.h"

#define SC_BYTEDELAY 25
#define SC_POSTDELAY 25
#define SC_SETUPDELAY 100

class StartupController
{

  public:
    StartupController(uint8_t ssPin);
    bool setupSPI();

	// LCD screen commands
    bool setup(uint8_t columns, uint8_t rows);
    bool begin(uint8_t columns, uint8_t rows);
    bool print(char str[]);
    bool print(int number, uint8_t base);
    bool print(int number);
	bool write(char chr);
    bool cursor();
    bool noCursor();
    bool cursorMode(bool mode);
    bool blink();
    bool noBlink();
    bool blinkMode(bool mode);
    bool display();
    bool noDisplay();
    bool displayMode(bool mode);
    bool scrollDisplayLeft();
    bool scrollDisplayRight();
    bool scroll(bool direction);
    bool autoScroll();
    bool noAutoScroll();
    bool autoScrollMode(bool mode);
    bool leftToRight();
    bool rightToLeft();
    bool textDirection(bool direction);
    bool createChar(uint8_t index, uint8_t chr[]);
    bool setCursor(uint8_t column, uint8_t row);
    bool home();
    bool clear();

	// controller commands
    bool setscreen(uint8_t id);
    bool renderBigNumber(uint8_t number, uint8_t column);
    bool clearBigNumber(uint8_t column);

    // Logger commands
    bool openLog(char logPrefix[]);
    bool newLog(char logPrefix[]);
    bool closeLog();
    bool clearLog();
    bool logWrite(char str[]);

	// controller interrupt commands
	int getFreeBuffer();

	void fileOpen(char filename[]);
	void fileClose();
	bool fileReadline(char destination[], uint8_t maxLength, char separator);
	void fileSeek(uint32_t location);
	bool fileExists(char filename[]);
	uint32_t fileSize(char filename[]);
	uint8_t getError();

  private:
	inline void _chipSelect();
	inline void _chipDeselect();
	inline void _bufferedCommandEnd();
    bool _transfer(uint8_t byte);
    byte _cTransfer(uint8_t byte);

	uint8_t _ssPin;
		  
};

#endif
